
#include "Base\stdafx.h"

#include "Bullet.h"
#include "Scenegraph/GameObject.h"

#include "../../Materials/ColorMaterial.h"
#include "../../Materials/DiffuseMaterial.h"
#include "..\..\Materials\SkinnedDiffuseMaterial.h"

#include "Components/ModelComponent.h"
#include "Components/ParticleEmitterComponent.h"

#include "Content/ContentManager.h"
#include "Physx/PhysxManager.h"
#include "Components/TransformComponent.h"
#include "Physx\PhysxProxy.h"

#include "Graphics\ModelAnimator.h"
#include "Graphics\TextureData.h"

#include "Scenegraph/GameScene.h"

Bullet::Bullet(XMFLOAT3 position):
		m_IsDead(false)
		, m_Position(position)
		,m_Count( 0 )
{
}

Bullet::~Bullet(void)
{
}

void Bullet::Initialize(const GameContext& gameContext)
{
	UNREFERENCED_PARAMETER(gameContext);

	GetTransform()->Translate(m_Position);
	AddComponent( new RigidBodyComponent() );
	shared_ptr<PxGeometry> Geom(new PxSphereGeometry(1) );
	auto mat = PhysxManager::GetInstance()->GetPhysics()->createMaterial(0.5f, 0.5f, 0.5f);
	auto collider = new ColliderComponent(Geom, *mat);

	AddComponent(collider);

	//level Material
	DiffuseMaterial* matDif = new DiffuseMaterial();
	matDif->SetDiffuseTexture(L"./Resources/Textures/metal.png");
	gameContext.pMaterialManager->AddMaterial(matDif, 645);

	//Level Model Component
	auto model = new ModelComponent(L"./Resources/Meshes/Bullet.ovm");
	model->SetMaterial(645);
	AddComponent(model);

	auto particleObj = new GameObject();
	auto particleEmitter = new ParticleEmitterComponent(L"./Resources/Textures/Smoke.png", 60);
	particleEmitter->SetVelocity(XMFLOAT3(0, 6.0f, 0));
	particleEmitter->SetMinSize(1.0f);
	particleEmitter->SetMaxSize(2.0f);
	particleEmitter->SetMinEnergy(1.0f);
	particleEmitter->SetMaxEnergy(2.0f);
	particleEmitter->SetMinSizeGrow(3.5f);
	particleEmitter->SetMaxSizeGrow(5.5f);
	particleEmitter->SetMinEmitterRange(0.2f);
	particleEmitter->SetMaxEmitterRange(0.5f);
	particleEmitter->SetColor(XMFLOAT4(1.f, 1.f, 1.f, 0.6f));
	AddComponent(particleEmitter);

}

void Bullet::UpdateCreate( const GameContext& gameContext )
{
	UNREFERENCED_PARAMETER(gameContext);

	GetTransform()->Translate(m_Position);
	auto rigidBody = new RigidBodyComponent();
	AddComponent( rigidBody );

	shared_ptr<PxGeometry> Geom(new PxSphereGeometry(1) );
	auto mat = PhysxManager::GetInstance()->GetPhysics()->createMaterial(0.5f, 0.5f, 0.5f);
	auto collider = new ColliderComponent(Geom, *mat);

	AddComponent(collider);

	//level Material
	DiffuseMaterial* matDif = new DiffuseMaterial();
	matDif->SetDiffuseTexture(L"./Resources/Textures/metal.png");
	gameContext.pMaterialManager->AddMaterial(matDif, 645);

	//Level Model Component
	auto model = new ModelComponent(L"./Resources/Meshes/Bullet.ovm");
	model->SetMaterial(645);
	AddComponent(model);

	auto particleObj = new GameObject();
	auto particleEmitter = new ParticleEmitterComponent(L"./Resources/Textures/Smoke.png", 60);
	particleEmitter->SetVelocity(XMFLOAT3(0, 6.0f, 0));
	particleEmitter->SetMinSize(1.0f);
	particleEmitter->SetMaxSize(2.0f);
	particleEmitter->SetMinEnergy(1.0f);
	particleEmitter->SetMaxEnergy(2.0f);
	particleEmitter->SetMinSizeGrow(3.5f);
	particleEmitter->SetMaxSizeGrow(5.5f);
	particleEmitter->SetMinEmitterRange(0.2f);
	particleEmitter->SetMaxEmitterRange(0.5f);
	particleEmitter->SetColor(XMFLOAT4(1.f, 1.f, 1.f, 0.6f));
	AddComponent(particleEmitter);

	GetTransform()->Scale(0.5f, 0.5f, 0.5f);
}

void Bullet::Timer( const GameContext& gameContext )
{
    m_Count += gameContext.pGameTime->GetElapsed();
	if (m_Count > 3)
	{
		m_IsDead = true;
	}
}

bool Bullet::IsDead()
{
	return m_IsDead;
}